package com.px.lovepath.domain.config;

import java.util.Date;
import java.util.HashMap;

import android.R.integer;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;


public class ConfigInfo {

	private static final String TAG = ConfigInfo.class.getName();
	private static ConfigInfo mSingleton = null;
	private Context mContext;
	private final SharedPreferences mPreferences;
	private final HashMap<Integer, Object> mConfig;
	
	private static final String SETTING_SP_NAME = "config";
	
	// ### 方法 ####
	public static void startUp(Context context) {
		assert mSingleton == null;
		mSingleton = new ConfigInfo(context);
	}
	public static ConfigInfo get() {
		assert mSingleton != null;
		return mSingleton;
	}
	public String getTaString() {
		if (getBoolean(ConfigKey.KEY_B_IS_MALE.getValue())) {
			return "她";
		} else {
			return "他";
		}
	}
	public String makeTaString(String original) {
		return String.format(original, getTaString());
	}
	public Date getTaBirthday() {
		Date date = new Date(getLong(ConfigKey.KEY_L_BIRTHDAY.getValue()));
		// 转为当天0点0分0秒
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		return date;
	}
	public void setTaBirthday(Date date) {
		setLong(ConfigKey.KEY_L_BIRTHDAY.getValue(), date.getTime());
	}
	public Date getFallLoveDay() {
		Date date = new Date(getLong(ConfigKey.KEY_L_FALLLOVEDAY.getValue()));
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		return date;
	}
	public void setFallLoveDay(Date date) {
		setLong(ConfigKey.KEY_L_FALLLOVEDAY.getValue(), date.getTime());
	}
	public String getUserName() {
		return getString(ConfigKey.KEY_S_USERNAME.getValue());
	}
	public void setUserName(String name) {
		setString(ConfigKey.KEY_S_USERNAME.getValue(), name);
	}
	public String getTaName() {
		return getString(ConfigKey.KEY_S_TA_NAME.getValue());
	}
	public void setTaName(String name) {
		setString(ConfigKey.KEY_S_TA_NAME.getValue(), name);
	}
	public String getTaPhoneNumber() {
		return getString(ConfigKey.KEY_S_TA_PHONE_NUMBER.getValue());
	}
	public void setTaPhoneNumber(String num) {
		setString(ConfigKey.KEY_S_TA_PHONE_NUMBER.getValue(), num);
	}
	public int getHomeSelect() {
		return getInt(ConfigKey.KEY_I_SELECT.getValue());
	}
	public void setHomeSelect(int select) {
		setInt(ConfigKey.KEY_I_SELECT.getValue(), select);
	}
	public boolean getBoolean(int key) {
		return (Boolean) mConfig.get(key);
	}
	public void setBoolean(int key, boolean value) {
		mConfig.put(key, value);
	}
	public long getLong(int key) {
		return (Long) mConfig.get(key);
	}
	public void setLong(int key, long value) {
		mConfig.put(key, value);
	}
	public String getString(int key) {
		return (String) mConfig.get(key);
	}
	public void setString(int key, String value) {
		mConfig.put(key, value);
	}
	public int getInt(int key) {
		return (Integer) mConfig.get(key);
	}
	public void setInt(int key, int value) {
		mConfig.put(key, value);
	}
	public void commit() {
		Editor editor = mPreferences.edit();
		editor.putLong(ConfigKey.KEY_L_FALLLOVEDAY.name(), getLong(ConfigKey.KEY_L_FALLLOVEDAY.getValue()));
		editor.putBoolean(ConfigKey.KEY_B_IS_MALE.name(), getBoolean(ConfigKey.KEY_B_IS_MALE.getValue()));
		editor.putString(ConfigKey.KEY_S_USERNAME.name(), getString(ConfigKey.KEY_S_USERNAME.getValue()));
		editor.putString(ConfigKey.KEY_S_TA_PHONE_NUMBER.name(), getString(ConfigKey.KEY_S_TA_PHONE_NUMBER.getValue()));
		editor.putString(ConfigKey.KEY_S_TA_NAME.name(), getString(ConfigKey.KEY_S_TA_NAME.getValue()));
		editor.putLong(ConfigKey.KEY_L_BIRTHDAY.name(), getLong(ConfigKey.KEY_L_BIRTHDAY.getValue()));
		editor.commit();
	}
	
	// ### 实现 ###
	private ConfigInfo(Context context) {
		mContext = context;
		mPreferences = mContext.getSharedPreferences(SETTING_SP_NAME, Context.MODE_PRIVATE);
		mConfig = new HashMap<Integer, Object>();
		getConfigFromFile();
	}
	private void getConfigFromFile() {
		long millis = System.currentTimeMillis();
		mConfig.put(ConfigKey.KEY_B_IS_MALE.getValue(), mPreferences.getBoolean(ConfigKey.KEY_B_IS_MALE.name(), true));
		mConfig.put(ConfigKey.KEY_L_BIRTHDAY.getValue(), mPreferences.getLong(ConfigKey.KEY_L_BIRTHDAY.name(), millis));
		mConfig.put(ConfigKey.KEY_L_FALLLOVEDAY.getValue(), mPreferences.getLong(ConfigKey.KEY_L_FALLLOVEDAY.name(), millis));
		mConfig.put(ConfigKey.KEY_S_USERNAME.getValue(), mPreferences.getString(ConfigKey.KEY_S_USERNAME.name(), ""));
		mConfig.put(ConfigKey.KEY_S_TA_NAME.getValue(), mPreferences.getString(ConfigKey.KEY_S_TA_NAME.name(), ""));
		mConfig.put(ConfigKey.KEY_S_TA_PHONE_NUMBER.getValue(), mPreferences.getString(ConfigKey.KEY_S_TA_PHONE_NUMBER.name(), ""));
		mConfig.put(ConfigKey.KEY_I_SELECT.getValue(), mPreferences.getInt(ConfigKey.KEY_I_SELECT.name(), 0));
	}
	
	public enum ConfigKey {
		KEY_B_IS_MALE(0),    				// 用户是否是男的
		KEY_L_BIRTHDAY(1),   				// 它生日
		KEY_L_FALLLOVEDAY(2), 				// 恋爱纪念日 
		KEY_S_USERNAME(3),                  // 用户昵称
		KEY_S_TA_PHONE_NUMBER(4),           // 它号码
		KEY_S_TA_NAME(5),                   // 它昵称
		KEY_I_SELECT(6);                    // 首页的选择 从上到下为0到3
		
		private int value;
		 
	    private ConfigKey(int value) {
	        this.value = value;
	    }
	    
	    public int getValue() {
	        return value;
	    }
	}
}
